<%@ page language="java" pageEncoding="UTF-8"%>

<script type="text/javascript">

var roleId = $.windowLocationSearch()["id"];
var bindedUserList;

function initBindUser() {
	bindedUserList = $("#bindedUserList").jslist({
		core: { 
			listUrl:ctx + "/biz/system/user/bind/list.json",
			countUrl:ctx + "/biz/system/user/bind/count.json",
			queryParams: {"roleId": roleId},
			columnMetadatas:[{width:100,title:"名称",name:"name"},
							{width:100,title:"编码",name:"account"},
							{width:100,title:"编码",name:"organName"},
							{title:"描述",name:"content"}]},
		selectable: {selectType:"checkbox"},
		plugins:["selectable"]
	})
	.bind("list_count_retrieved.jslist",function(event,data) {
		$("#bindedUserListSummary").html("("+data.retrievedCount+"/"+data.totalCount+"条)"); 
	})
	.jslist("refresh");
};

function bindUser() {
	$.openDialog("#dialog",
			{loadPage:"/biz/system/user/unbind/query.jsp?roleId="+roleId,
			title:"选择用户"},
			function(data) {
				refreshUsers();
			});
}

function unbindUser() {
	var selectedItems = bindedUserList.jslist("getSelectedItems");//$("#queryResultBody").find(":checkbox:checked");
	if (selectedItems.length == 0) {
		return;
	}
	
	if (!confirm('是否取消绑定？\n请确认是否继续操作 ！')) {
		return;
	}
	
	var userIds = $(selectedItems).map(function(i,item){
		return item.id;
	}).get().join(",");
	
	onUnbindUser(userIds);
}

function onUnbindUser(userIds) {
	$.ajax({
		type: "POST",
		url: "unbindUser.json",
		data: {
			"roleId":roleId,
			"userIds":userIds
		},
		dataType: "json",
		success: function(data){
			$.alert("已取消绑定");
			refreshUsers();
		},
		error:function(message){
			$.alert(e);
		}
	});
}
 
function refreshUsers() {
	bindedUserList.jslist("refresh");
}

</script>

<div id="bindedUserList">
</div>

<div class="form-actions">
	<a href="#" class="btn btn-primary" onclick="bindUser()"><i class="icon-ok"></i>绑定用户</a>
	<a href="#" class="btn btn-warning" onclick="unbindUser()"><i class="icon-ok"></i>取消绑定</a>
	<a href="#" class="btn" onclick="refreshUsers()"><i class="icon-ok"></i>刷新</a>
</div>
 